package com.epam.rd.my_classLoader;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;


public class Watcher {
	public void scanFolder(String url){
		Path path = Paths.get(url);
        WatchService watchService = null;
        try {
                watchService = path.getFileSystem().newWatchService();
                path.register( watchService, StandardWatchEventKinds.ENTRY_CREATE,
                                StandardWatchEventKinds.ENTRY_DELETE,
                                StandardWatchEventKinds.ENTRY_MODIFY );
        }
        catch( IOException e1 ) {
                e1.printStackTrace();
        }
        
        for( ;; ) {
                WatchKey key = null;
                try {
                        key = watchService.take();
                }
                catch( InterruptedException e ) {
                        e.printStackTrace();
                }
                for( WatchEvent event : key.pollEvents() ) {
                        switch( event.kind().name() ) {
                                case "ENTRY_MODIFY":
                                        	 if( event.context().toString()
                                                     .matches( "[a-zA-Z]+\u002Ejava" ) ) {	
                                                try {
                                                	System.out.println("heedsdfasdfasd");
													JavaFileCompiler.compileJava("TestModule.java");
												} catch (IOException e) {
													e.printStackTrace();
												}
                                        }
                                        break;
                        }
                }
                key.reset();
        }
	}
}
